<@pp.dropOutputFile/>

<#-- generate package name -->

<#-- iterate table list-->
<#list doc.tables as table>
<#assign module = doc.artifactid?replace(".", "/") + "/" + table.moduleid?uncap_first>
<#assign fileName = table.name?cap_first>
<@pp.changeOutputFile name="/main/webapp/" + module + "/" + fileName + "/test.html"/>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv=Content-Type content="text/html; charset=<#if doc.encoding??>${doc.encoding}<#else>gb2312</#if>">
<title>${doc.artifactid} test!</title>
<#assign root = "/erpsupport">
<link href="${root}/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>

<script type="text/javascript" src="${root}/plugins/jquery-easyui/jquery.min.js" charset="<#if doc.encoding??>${doc.encoding}<#else>gb2312</#if>"></script>
<script type="text/javascript">
$(function(){});

jQuery.unserialize = function(str){
		var items = str.split('&');
		var ret = "{";
		var arrays = [];
		var index = "";
		for (var i = 0; i < items.length; i++) {
			var parts = items[i].split(/=/);
			//console.log(parts[0], parts[0].indexOf("%5B"),  parts[0].indexOf("["));
			if (parts[0].indexOf("%5B") > -1 || parts[0].indexOf("[") > -1){
				//Array serializado
				index = (parts[0].indexOf("%5B") > -1) ? parts[0].replace("%5B","").replace("%5D","") : parts[0].replace("[","").replace("]","");
				//console.log("array detectado:", index);
				//console.log(arrays[index] === undefined);
				if (arrays[index] === undefined){
					arrays[index] = [];
				}
				arrays[index].push( decodeURIComponent(parts[1].replace(/\+/g," ")));
				//console.log("arrays:", arrays);
			} else {
				//console.log("common item (not array)");
				if (parts.length > 1){
					ret += "\""+parts[0] + "\": \"" + decodeURIComponent(parts[1].replace(/\+/g," ")).replace(/\n/g,"\\n").replace(/\r/g,"\\r") + "\", ";
				}
			}
			
		};
		
		ret = (ret != "{") ? ret.substr(0,ret.length-2) + "}" : ret + "}";
		//console.log(ret, arrays);
		var ret2 = JSON.parse(ret);
		//proceso los arrays
		for (arr in arrays){
			ret2[arr] = arrays[arr];
		}
		return ret2;
}

jQuery.fn.unserialize = function(parm){
		//If not string, JSON is assumed.
		var items = (typeof parm == "string") ? parm.split('&') : parm;
		if (typeof items !== "object"){
			throw new Error("unserialize: string or JSON object expected.");
		}
		//Check for the need of building an array from some item.
		//May return a false positive, but it's still better than looping twice.
		//TODO: confirm if it's ok to simplify this method by always calling
		//$.unserialize(parm) without any extra checking. 
		var need_to_build = ((typeof parm == "string") && decodeURIComponent(parm).indexOf("[]=") > -1);
		items = (need_to_build) ? $.unserialize(parm) : items;
		
		
		for (var i in items){
			var parts = (items instanceof Array) ? items[i].split(/=/) : [i, (items[i] instanceof Array) ? items[i] : "" + items[i]];
			parts[0] = decodeURIComponent(parts[0]);
			if (parts[0].indexOf("[]") == -1 && parts[1] instanceof Array){
				parts[0] += "[]";
			}
			obj = this.find('[name=\''+ parts[0] +'\']');
			if (obj.length == 0){
				try{
					obj = this.parent().find('[name=\''+ parts[0] +'\']');
				} catch(e){}
			}
			if (typeof obj.attr("type") == "string" && ( obj.attr("type").toLowerCase() == "radio" || obj.attr("type").toLowerCase() == "checkbox")){
				 obj.each(function(index, coso) {
					coso = $(coso);
					//if the value is an array, i gotta search the item with that value.
					if (parts[1] instanceof Array){
						for (var i2 in parts[1]){
							var val = ""+parts[1][i2];
							if (coso.attr("value") == decodeURIComponent(val.replace(/\+/g," "))){
								coso.prop("checked",true);
							} else {
								if ($.inArray(coso.val(),parts[1]) < 0){
									coso.prop("checked",false);
								}
							}
						}
					} else {
						val = "" + parts[1];
						if (coso.attr("value") == decodeURIComponent(val.replace(/\+/g," "))){
							coso.prop("checked",true);
						} else {
							coso.prop("checked",false);
						}
					}
				 });
			} else if (obj.length > 0 && obj[0].tagName == "SELECT" && parts[1] instanceof Array && obj.prop("multiple")){
				//Here, i have an array for a multi-select.
				obj.val(parts[1]);
			} else {
				//When the value is an array, we join without delimiter
				var val = (parts[1] instanceof Array) ? parts[1].join("") : parts[1];
				//when the value is an object, we set the value to ""
				val = (typeof val == "object") ? "" : val;
				
				obj.val(decodeURIComponent(val.replace(/\+/g," ")));
			}
		};
		return this;
}
</script>
</head>
<body>

	<div class="container">
		<h1>${fileName} test!</h1>

		<div class="row">
			<div class="col-md-6">
				<div class="panel panel-primary">
					<div class="panel-heading">create.action</div>
					<div class="panel-body">
						<script type="text/javascript">
							function add(){
								$.post("${root}/${module}/${fileName}/create.action",
								$("#create_form").serializeArray(),
								function(data){
									$("#_create_result").html(JSON.stringify(data));
								},
								"json");
							}
						</script>
						<form class="form" id="create_form">
							<#list table.fields as field>
							<div class="input-group">
								<div class="input-group-addon">${field.name?uncap_first}</div>
								<input type="text" class="form-control" name="obj.${field.name?uncap_first}" id="_${field.name?uncap_first}_create" value="1" placeholder="${field.name?uncap_first}"/>
							</div>
							</#list>
							<button type="button" id="_create" class="btn btn-primary" onclick="add()">create</button>
						</form>
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="panel panel-warning">
					<div class="panel-heading">result!</div>
					<div class="panel-body">
						<div id="_create_result"></div>
					</div>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="panel panel-primary">
					<div class="panel-heading">import.action</div>
					<div class="panel-body">
						<script type="text/javascript">
							function importData(){
								$.post("${root}/${module}/${fileName}/import.action",
								$("#import_form").serializeArray(),
								function(data){
									$("#_import_result").html(JSON.stringify(data));
								},
								"json");
							}
						</script>
						<form class="form" id="import_form">
							<#list table.fields as field>
							<div class="input-group">
								<div class="input-group-addon">${field.name?uncap_first}</div>
								<input type="text" class="form-control" name="objs[0].${field.name?uncap_first}" id="_${field.name?uncap_first}_create" value="1" placeholder="${field.name?uncap_first}"/>
							</div>
							</#list>
							<#list table.fields as field>
							<div class="input-group">
								<div class="input-group-addon">${field.name?uncap_first}</div>
								<input type="text" class="form-control" name="objs[1].${field.name?uncap_first}" id="_${field.name?uncap_first}_create" value="2" placeholder="${field.name?uncap_first}"/>
							</div>
							</#list>
							<button type="button" id="_import" class="btn btn-primary" onclick="importData()">import Data</button>
						</form>
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="panel panel-warning">
					<div class="panel-heading">result!</div>
					<div class="panel-body">
						<div id="_import_result"></div>
					</div>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="panel panel-primary">
					<div class="panel-heading">get.action</div>
					<div class="panel-body">
						<script type="text/javascript">
							function get(){
								$.post("${root}/${module}/${fileName}/get.action",
								{id : $("#_id_get").val()},
								function(data){
									$("#_get_result").html(JSON.stringify(data));
								},
								"json");
							}
						</script>
						<form class="form-inline">
							<div class="form-group">
								<div class="input-group">
									<div class="input-group-addon">id</div>
									<input type="text" class="form-control" id="_id_get" value="1" placeholder="id"/>
								</div>
							</div>
							<button type="button" id="_get" class="btn btn-primary" onclick="get()">get</button>
						</form>
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="panel panel-warning">
					<div class="panel-heading">result!</div>
					<div class="panel-body">
						<div id="_get_result"></div>
					</div>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="panel panel-primary">
					<div class="panel-heading">getPage.action</div>
					<div class="panel-body">
						<script type="text/javascript">
							function getPage(){
								$.post("${root}/${module}/${fileName}/getPage.action",
								{	
									page : $("#_page_getPage").val(),
									rows : $("#_rows_getPage").val()
								},
								function(data){
									$("#_getPage_result").html(JSON.stringify(data));
								},
								"json");
							}
						</script>
						<form class="form-inline">
							<div class="form-group">
								<div class="input-group">
									<div class="input-group-addon">page</div>
									<input type="text" class="form-control" id="_page_getPage" value="1" placeholder="page"/>
								</div>
								<div class="input-group">
									<div class="input-group-addon">rows</div>
									<input type="text" class="form-control" id="_rows_getPage" value="3" placeholder="rows"/>
								</div>
							</div>
							<button type="button" id="_getPage" class="btn btn-primary" onclick="getPage()">getPage</button>
						</form>
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="panel panel-warning">
					<div class="panel-heading">result!</div>
					<div class="panel-body">
						<div id="_getPage_result"></div>
					</div>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="panel panel-primary">
					<div class="panel-heading">get.action</div>
					<div class="panel-body">
						<script type="text/javascript">
							function getAll(){
								$.post("${root}/${module}/${fileName}/getAll.action",
								{},
								function(data){
									$("#_getAll_result").html(JSON.stringify(data));
								},
								"json");
							}
						</script>
						<form class="form-inline">
							<button type="button" id="_getAll" class="btn btn-primary" onclick="getAll()">getAll</button>
						</form>
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="panel panel-warning">
					<div class="panel-heading">result!</div>
					<div class="panel-body">
						<div id="_getAll_result"></div>
					</div>
				</div>
			</div>
		</div>
		
		<div class="row">
			<div class="col-md-6">
				<div class="panel panel-primary">
					<div class="panel-heading">update.action</div>
					<div class="panel-body">
						<script type="text/javascript">
							function json2form(){
								$.post("${root}/${module}/${fileName}/get.action",
								{id : $("#_id_load").val()},
								function(data){
									if(data.rows.length > 0){
										$("#update_form").unserialize(data.rows[0]);
									}
								},
								"json");
							}
							
							function update(){
								$.post("${root}/${module}/${fileName}/update.action",
								$("#update_form").serializeArray(),
								function(data){
									$("#_update_result").html(JSON.stringify(data));
								},
								"json");
							}
						</script>
						<form class="form-inline">
							<div class="form-group">
								<div class="input-group">
									<div class="input-group-addon">id</div>
									<input type="text" class="form-control" id="_id_load" value="1" placeholder="id"/>
								</div>
							</div>
							<button type="button" id="_load" class="btn btn-primary" onclick="json2form()">load</button>
						</form>

						<form class="form" id="update_form">
							<#list table.fields as field>
							<div class="input-group">
								<div class="input-group-addon">${field.name?uncap_first}</div>
								<input type="text" class="form-control" id="_${field.name?uncap_first}_update" name="obj.${field.name?uncap_first}" value="1" placeholder="${field.name?uncap_first}"/>
							</div>
							</#list>
							<button type="button" id="_update" class="btn btn-primary" onclick="update()">update</button>
						</form>
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="panel panel-warning">
					<div class="panel-heading">result!</div>
					<div class="panel-body">
						<div id="_update_result"></div>
					</div>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="panel panel-primary">
					<div class="panel-heading">deleteItems.action</div>
					<div class="panel-body">
						<script type="text/javascript">
							
							function deleteItems(){
								var items = $("#_ids_deleteItems").val().trim().split(",");
								var ids = new Array();
								for(i in items){
									ids.push("items=" + items[i]);
								}
								$.post("${root}/${module}/${fileName}/deleteItems.action?" + ids.join("&"),
								{},
								function(data){
									$("#_deleteItems_result").html(JSON.stringify(data));
								},
								"json");
							}
						</script>

						<form class="form" id="_deleteItems_form">
							<div class="form-group">
								<div class="input-group">
									<div class="input-group-addon">ids</div>
									<input type="text" class="form-control" id="_ids_deleteItems" value="1" placeholder="ids"/>
								</div>
							</div>
							<button type="button" id="_deleteItems" class="btn btn-primary" onclick="deleteItems()">deleteItems</button>
						</form>
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="panel panel-warning">
					<div class="panel-heading">result!</div>
					<div class="panel-body">
						<div id="_deleteItems_result"></div>
					</div>
				</div>
			</div>
		</div>

	</div>

</body>
</html>
</#list>